package com.jianglin.mapper;

import com.jianglin.model.House;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component("houseDao")
public interface HouseMapper{
    @Select("<script>" +
            "SELECT * FROM house" +
            "<where>" +
            "   <if test='numbers != null and numbers != \"\"'>" +
            "       AND numbers LIKE CONCAT('%', #{numbers}, '%')" +
            "   </if>" +
            "</where>" +
            "</script>")
    List<House> findHouseAll(@Param("numbers") String numbers);

    @Select("SELECT * FROM house WHERE id = #{houId}")
    House queryHouseById(@Param("houId") Integer houId);

    // 新增基本 CRUD 方法
    @Select("SELECT * FROM house")
    List<House> selectList();

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("INSERT INTO house (storey, numbers, status, into_date, building_id, remarks, area) " +
            "VALUES (#{storey}, #{numbers}, #{status}, #{intoDate}, #{buildingId}, #{remarks}, #{area})")
    int insert(House house);

    @Delete("DELETE FROM house WHERE id = #{id}")
    int deleteById(Integer id);

    @Update("UPDATE house SET " +
            "storey = #{storey}, " +
            "numbers = #{numbers}, " +
            "status = #{status}, " +
            "into_date = #{intoDate}, " + // 注意这里改为数据库字段名 into_date
            "building_id = #{buildingId}, " +
            "remarks = #{remarks}, " +
            "area = #{area} " +
            "WHERE id = #{id}")
    int updateById(House house);

    @Select("SELECT * FROM house WHERE id = #{id}")
    House selectById(Integer id);
}
